//
//  KNBaseRequest.h
//  Backhome
//
//  Created by kwep_vbn on 2017/3/22.
//  Copyright © 2017年 SmartHome. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "KNNetworkingManager.h"
#import <MBProgressHUD/MBProgressHUD.h>

typedef enum : NSUInteger {
    KNRequestMethodPOST = 1,
    KNRequestMethodGET,
} KNRequestMethod;


typedef enum : NSInteger {
    KNNetworkStatusNone = -1,
    KNNetworkStatus2G = 1,
    KNNetworkStatus3G,
    KNNetworkStatus4G,
    KNNetworkStatusWIFI,
} KNNetworkStatus;

typedef NS_ENUM(NSInteger, KNProgressHUDMode) {
    KNProgressHUDModeIndeterminate,
    /// A round, pie-chart like, progress view.
    KNProgressHUDModeDeterminate,
    /// Horizontal progress bar.
    KNProgressHUDModeDeterminateHorizontalBar,
    /// Ring-shaped progress view.
    KNProgressHUDModeAnnularDeterminate,
    /// Shows a custom view.
    KNProgressHUDModeCustomView,
    /// Shows only labels.
    KNProgressHUDModeText
};


@interface KNBaseAPIRequest : NSObject

@property (strong , nonatomic) UIView *hudContainerView; // default is window

@property (strong, nonatomic) NSString *errorText; // 网络不太稳定呦~~

// 发起请求返回的task
@property (strong, nonatomic) NSURLSessionDataTask *currentTask;

/**
 通用url,如果需要请求不同服务器的接口，重载
 */

@property (nonatomic,copy) NSString *baseUrl;

/**
 资源url,如果需要请求不同接口，重载
 */
@property (nonatomic,copy) NSString *requestUrl;

/**
 默认GET
 */
@property (nonatomic,assign) KNRequestMethod requestMethod;

/**
 默认json
 */
@property (nonatomic,assign) KNRequestSerializer requestSerilizer;

/**
 默认json
 */
@property (nonatomic,assign) KNResponseSerializer responseSerializer;

/**
 超时，默认60
 */
@property (nonatomic,assign) NSInteger timeout;

/**
 请求时是否显示hud
 */
@property (nonatomic,assign) BOOL needShowRequestHud;

/**
 是否在请求失败后显示错误信息
 */
@property (nonatomic,assign) BOOL needShowRequestFaildMsg;

/**
 hud文字
 */
@property (nonatomic,copy) NSString *hudText;

/**
 hud 模式
 */
@property (nonatomic,assign) KNProgressHUDMode hudMode;

@property (nonatomic,assign) BOOL userInteractionEnabled; // default is NO

@property (assign, nonatomic) BOOL needRemoveHUDContainerView;

/**
 请求参数
 */
@property (strong, nonatomic) id parmeters;

/**
 通用头
 */
@property (strong, nonatomic) NSMutableDictionary *headers;

/**
 通用参数
 */
@property (strong, nonatomic) id commonParmeters;


/**
  自动生成参数，前缀必须为auto_parms_xxxxxx（适用于简单的参数拼接）,与手动填写的parmeters冲突，二选一
 */
@property (nonatomic,assign) BOOL autoBuildParms;


/**
 类方法构建
 */
+ (KNBaseAPIRequest *)createRequestWithMethod:(KNRequestMethod)method url:(NSString *)url parms:(id)parms;

/**
 发起请求
 */
- (NSURLSessionDataTask *)requestWithSuccess:(KNNetworkingSuccess)success faild:(KNNetworkingError)faild;

- (NSURLSessionDataTask *)uploadFile:(NSData *)data
                            fileName:(NSString *)fileName
                            mimeType:(NSString *)mimeType
                             success:(KNNetworkingSuccess)success
                               faild:(KNNetworkingError)faild;

- (NSURLSessionTask *)downloadFileWithProgress:(nullable void (^)(NSProgress *downloadProgress))progress
                       destination:(nullable NSURL*(^)(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response))destination
                           success:(KNNetworkingSuccess)success
                           failure:(KNNetworkingError)failure;

/**
 可以重载判断接口请求
 */
- (NSError *)haveError:(id)response;

// 获取网络状态
- (KNNetworkStatus)getNetWorkStates;

- (void)cancel;

- (void)processResult:(id)result response:(NSURLResponse *)response successBlock:(KNNetworkingSuccess)success faild:(KNNetworkingError)faild hud:(MBProgressHUD *)hud;

- (void)processError:(NSError *)error response:(NSURLResponse *)response faild:(KNNetworkingError)faild hud:(MBProgressHUD *)hud;



@end
